package com.yifeng.repo.base.api;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by daibing on 2023/8/6.
 */
@Data
@AllArgsConstructor
public class TimeRange {
    private LocalTime start;
    private LocalTime end;

    /**
     * 09:00~11:30,14:00~17:30,19:00~20:30
     */
    public static List<TimeRange> parse(String timeRangesText) {
        if (timeRangesText == null || timeRangesText.trim().length() == 0) {
            return Collections.emptyList();
        }
        String[] timeRangeTextList = timeRangesText.split(",");
        List<TimeRange> timeRangeList = new ArrayList<>(timeRangeTextList.length);
        for (String s : timeRangeTextList) {
            String[] timeTextList = s.split("~");
            timeRangeList.add(new TimeRange(LocalTime.parse(timeTextList[0]), LocalTime.parse(timeTextList[1])));
        }
        return timeRangeList;
    }

    public static boolean nowInside(List<TimeRange> timeRangeList) {
        LocalTime now = LocalTime.now();
        return timeRangeList.stream().anyMatch(t -> now.isAfter(t.getStart()) && now.isBefore(t.getEnd()));
    }
}
